<?php
class Admin_Form_ValidateNewsItem extends Zendvn_Validate_Form{

	public function __construct($arrParam = null, $options = null){
		
		define('NAME_ID',$arrParam['id']);
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
												'fileConfig'=>'validates/news-item.ini'));
		if($arrParam['action']=='edit'){
			$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
					'fileConfig'=>'validates/news-item.ini',
					'section'=>'edit'));
		}
		//================================Validate Name================================
		$name  = $config->getConfig('name');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['name'])){
			$message = $validator->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}
		//================================Validate summary================================

		$name  = $config->getConfig('summary');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['summary'])){
			$message = $validator->getMessages();
			$this->_arrError['summary'] = current($message);
			$arrParam['summary'] = '';
		}
		//================================Validate content================================

		$name  = $config->getConfig('content');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['content'])){
			$message = $validator->getMessages();
			$this->_arrError['content'] = current($message);
			$arrParam['content'] = '';
		}
		//================================Validate tags================================
		if($arrParam['tags']){
			$name  = $config->getConfig('tags');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['tags'])){
				$message = $validator->getMessages();
				$this->_arrError['tags'] = current($message);
				$arrParam['tags'] = '';
			}
		}
		//================================Validate Html_descript================================
		if($arrParam['html_descript']){
			$name  = $config->getConfig('html_descript');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['html_descript'])){
				$message = $validator->getMessages();
				$this->_arrError['html_descript'] = current($message);
				$arrParam['html_descript'] = '';
			}
		}
		//================================Validate html_keyword================================
		if($arrParam['html_keyword']){
			$name  = $config->getConfig('html_keyword');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['html_keyword'])){
				$message = $validator->getMessages();
				$this->_arrError['html_keyword'] = current($message);
				$arrParam['html_keyword'] = '';
			}
		}
		//================================Validate hits================================		

		if($arrParam['hits']){
			$name = $config->getConfig('hits');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['hits'])){
				$message = $validator->getMessages();
				$this->_arrError['hits'] = current($message);
				$arrParam['hits'] = '';
			}
		}
		//================================Validate Ordering================================		

		if($arrParam['ordering']){
			$name = $config->getConfig('ordering');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['ordering'])){
				$message = $validator->getMessages();
				$this->_arrError['ordering'] = current($message);
				$arrParam['ordering'] = '';
			}
		}
		/*=======================================================
		 * Start - validate picture
		 *=======================================================*/
		$error = Admin_Form_NewsItem_ValidateImage::validate($arrParam, $options);
		if($error != ''){
			$this->_arrError['picture'] = $error;
			$arrParam['picture'] = '';
		}else{
			if($arrParam['action'] == 'edit'){
				$this->_arrData['picture'] = $arrParam['current_picture'];
			}
		}
		if(empty($arrParam['cat_id'])){
			$this->_arrError['cat_id'] = 'Please select a category!';
		}
		/*=======================================================
		 * Start - validate file_attach
		 *=======================================================*/
		/*
		$error = Admin_Form_NewsItem_ValidateFileAttach::validate($arrParam, $options);
		if($error != ''){
			$this->_arrError['file_attach'] = $error;
			$arrParam['file_attach'] = '';
		}else{
			if($arrParam['action'] == 'edit'){
				$this->_arrData['current_file_attach'] = $arrParam['current_file_attach'];
			}
		}*/
		/*=======================================================
		 * Start - return correct data array
		 *=======================================================*/
		$this->_arrData = $arrParam;
	}
	
	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		
		}
		return $this->_arrData;
	}
	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0 && $this->_arrError != ''){
			$flag = true;
		}
		return $flag;
	}
	
	public function getMessageErrors(){
		return $this->_arrError;
	}
	
	public function upload(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'],
													 	  'fileConfig'=>'news-category.ini'));
		$itemConfig = $config->getConfig('item');
			
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];
		$filter = new Zendvn_Filter_FileName();
		if(!empty($fileName)){		
			$options = array('rename'=> true,'prefix'=>$filter->filter($fileName));			
			$fileName = $upload->upload($itemConfig['uploadDir'],'picture',$options);			
			$arrFile['picture'] = $fileName;
			
			$arrParam = array(
							'uploadDir' => $itemConfig['uploadDir'],
							'thumb-width' => array($itemConfig['thumb-width1'], $itemConfig['thumb-width2'], $itemConfig['thumb-width3']),
							'thumb-height' => array($itemConfig['thumb-height1'], $itemConfig['thumb-height2'], $itemConfig['thumb-height3']),
							'thumbDir'	=> array($itemConfig['thumbDir1'], $itemConfig['thumbDir2'], $itemConfig['thumbDir3'])
							);
			
			$options = array('type' => array($itemConfig['thumbResize1'], $itemConfig['thumbResize2'], $itemConfig['thumbResize3']));
			$img = Zendvn_File_Image::resizeImg($fileName, $arrParam, $options);
			
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($itemConfig['uploadDir'],$this->_arrData['current_picture']);
				$upload->remove($itemConfig['thumbDir1'],$this->_arrData['current_picture']);
				$upload->remove($itemConfig['thumbDir2'],$this->_arrData['current_picture']);
				$upload->remove($itemConfig['thumbDir3'],$this->_arrData['current_picture']);
			}
		
		}		
		/*
		$fileInfo = $upload->getFileInfo('file_attach');
		$fileName = $fileInfo['file_attach']['name'];
		if(!empty($fileName)){			
			$options = array('rename'=> true,'prefix'=>$filter->filter($fileName));	
			$fileName = $upload->upload($itemConfig['fileAttachDir'],'file_attach',$options);
			$arrFile['file_attach'] = $fileName;
			if($this->_arrData['action'] == 'edit'){				
				$upload->remove($itemConfig['fileAttachDir'],$this->_arrData['current_file_attach']);
			}
		}*/
		return $arrFile;
	}
}







